package com.fj.controller;

import com.fj.entity.BackendUser;
import com.fj.service.BackendUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class BackendUserController {

    @Autowired
    private BackendUserService backendUserService;

    @RequestMapping("/backendLogin")
    public String backendLogin(HttpServletRequest request, Model model,String userCode,String userPwd){
        if(userCode==null || userCode==""){
            model.addAttribute("code","登录名不能为空");
            return "backendlogin";
        }
        if(userPwd==null || userPwd==""){
            model.addAttribute("pwd","密码不能为空");
            return "backendlogin";
        }
        BackendUser backendUser = backendUserService.login(userCode);
        if(backendUser==null){
            model.addAttribute("backendUser","登录名错误或不存在");
            return "backendlogin";
        }
        if(!backendUser.getUserpassword().equals(userPwd)){
            model.addAttribute("dlpwd","密码错误");
            return "backendlogin";
        }
        HttpSession session=request.getSession();
        session.setAttribute("userName",backendUser.getUsername());
        return "backendList";
    }
}
